$(function(){
	$("img").lazyload({
		effect : "fadeIn"
	}); 
	
	var data = {};	//搜索所需参数
	initSearch();
	/*-- 筛选 Start --*/

	$(".choose").on("click", "a", function() {
		var clickContent = $(this).text().substring(0,$(this).text().length-1);
		$(this).remove();
		if($(".choose").children().size()==0){
			$(".del-all.del-btn").hide();
		}
		 $("span:contains("+clickContent+")").parents(".select").children(".unlimited").click();
	});			

	$(".select").on("click", "a", function(){
		var selectType = $(this).parents("li").attr('class');
		switch(selectType){
			case "brand":
			  $("#brandId").val($(this).attr("data-id"));
			  $("#brandName").val($(this).text());
			  break;
			case "price":
				var priceM = $(this).attr("data-m");
				var	priceL = $(this).attr("data-l");
				var	priceId = $(this).attr("data-id");
				$("#priceStart").val(priceM);
				$("#priceEnd").val(priceL);
				$("#priceId").val(priceId);
			  break;
			case "try_price":
				var priceM = $(this).attr("data-m");
				var	priceL = $(this).attr("data-l");
				var	priceId = $(this).attr("data-id");
				$("#tryPriceStart").val(priceM);
				$("#tryPriceEnd").val(priceL);
				$("#tryPriceId").val(priceId);
			  break;
			case "type":
				$("#type").val($(this).attr("data-id"));
			  break;
			}
		
		$("#goodsList").submit();
	});

	/*-- 筛选 End --*/


	//收起展开
	$(".show-more").click(function(){
		$(this).parents(".ls-valude ").toggleClass("open close");
		$(".show-more i").toggleClass("icon-arrow-up icon-arrow-down");
		if($(this).find("em").html() == "展开"){
			$(".show-more em").html("收起");
		}else{
			$(".show-more em").html("展开");
		}
	});

	//排序
	$("#listSort .sortbtn ").on("click",function(){
		var dataId = $(this).attr("data-id");
		$("[name='sort_type']").val($(this).attr("data-id"));
		if($(this).hasClass("desc")){
			$(this).addClass("asc").siblings().removeClass("desc asc");
			$(this).children().addClass("icon-arrow-asc-white").removeClass("icon-arrow-desc-grey icon-arrow-desc-white");
			$("[name='sort']").val("ASC");
		}else{
			$(this).addClass("desc").siblings().removeClass("desc asc");
			$(this).children().addClass("icon-arrow-desc-white").removeClass("icon-arrow-desc-grey icon-arrow-asc-white");
			$(this).siblings().children().addClass("icon-arrow-desc-grey").removeClass("icon-arrow-asc-white icon-arrow-desc-white");
			$("[name='sort']").val("DESC");
		}
		$("#goodsList").submit();
	})
	//是否有货
	$("#isHasGoods").on("click",function(){
		if($(this).hasClass("stock-select")){
			$("[name='stock']").val(0);
		}else{
			$("[name='stock']").val(1);
		}
		$("#goodsList").submit();
	});

	//根据首字母获取品牌名称
	$(".letter-list a").on("click",function(e){
		$(this).addClass("cur").siblings().removeClass("cur");
		var brandType = $(this).attr("data-id");
		var obj = {};
		$("input").each(function(){
			var idName = $(this).attr('id');
			
			obj[idName] = $(this).val();
		});
		$.doAjax({
				url: requestBrandUrl,
				type:"post",
				dataType:"html",
				data:{
					"brandtype":brandType,	//字母
					"goods_name":goodsName,	//商品名称
					"category":obj.category,	//分类
					"brand_id":obj.brandId,		//品牌id
					"brand_name":obj.brandName,			//品牌名称
					"price_id":obj.priceId,
					"price_start":obj.priceStart,			//开始价格
					"price_start":obj.priceStart,			//结束价格
					"try_price_id":obj.tryPriceId,			//试豆价格
					"try_price_start":obj.tryPriceStart,			//试豆开始价格
					"try_price_end":obj.tryPriceEnd,			//试豆结束价格
					"type":obj.type,	//类型
					"sort_type":obj.sortType,
					"sort":obj.sort,
					"stock":obj.stock
				},
				success: function(data){
					if(data.length !== 0){
						$(".brand-list").html("").append('<a data-id="-1" class="unlimited cur"><span>不限</span></a>'+ data);
					}else{
						$(".brand-list").html('<p class="error">品牌为空</p>');
					}
				}
			});
		return false;
	})
})

//初始化搜索条件样式
function initSearch(){
	var searchTags = "";
	$("#listSelect ul li").each(function(){
		var id = searchData[$(this).attr("class")];
		var selected = $(this).find("[data-id='"+id+"']");
		selected.addClass("cur").siblings().removeClass("cur");
		if(id!=0 && selected.text() != "不限"&& selected.text() !=""){
			searchTags += "<a class='"+ $(this).attr("class")+"-tags'>"+selected.text()+"<em class='del'>&times;</em></a>";
			$('.del-all').show();
		}
		
	});
	$("#searchTags").html(searchTags);
	if(sort_type){
		var sortClass = sort.toLowerCase();
		$("#listSort .sortbtn").removeClass("desc asc");
		$("#listSort .sortbtn i").removeClass().addClass("icon icon-arrow-desc-grey");
		$("#listSort span.sortbtn"+sort_type).addClass(sortClass);
		$("#listSort span.sortbtn"+sort_type +" i").removeClass().addClass("icon icon-arrow-"+sortClass + "-white");
	}
}
